package com.fqcar.fqdf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Response;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.core.BaseRequestActivity;
import com.fqcar.fqdf.entity.UserRespBean;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqcar.fqdf.util.Constants;
import com.fqcar.fqdf.util.SharedPreferenceUtil;
import com.fqdf.framework.android.net.response.NetBeanWrapper;
import com.fqdf.framework.android.util.CheckLogicUtil;
import com.fqdf.framework.android.util.DialogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by lvsidong on 16/2/29.
 */
public class LoginActivity extends BaseRequestActivity {

    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.sms_code)
    EditText sms_code;
    @BindView(R.id.login_btn)
    Button login;
    private String login_out_str;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initData();
    }

    public void initData() {
        if (null != getIntent().getStringExtra("content")) {
            login_out_str = getIntent().getStringExtra("content");
            DialogUtil.ToastMessage(LoginActivity.this, login_out_str);
        }
    }

    /**
     * 登录
     */
    public void login() {
        String sim = phone.getEditableText().toString();
        String code = sms_code.getEditableText().toString();
//        cityCode = ((GoFunApp)getApplication()).mLocation.getCityCode();
        if (!sim.matches("[1][0123456789]\\d{9}")) {
            DialogUtil.ToastMessage(LoginActivity.this, getResources().getString(R.string.error_phone_number));
            return;
        } else if(CheckLogicUtil.isEmpty(code)){
            DialogUtil.ToastMessage(LoginActivity.this, getResources().getString(R.string.error_phone_pwd_empty));
            return;
        }else {
            getProgressDialog().show();

            /*RequestOptService.login(LoginActivity.this, sim, code, new Response.Listener<ResponseEntity>() {
                @Override
                public void onResponse(ResponseEntity response) {
                    getProgressDialog().dismiss();

                    String token = response.modelData.get("token").toString();
                    if (!CheckLogicUtil.isEmpty(token)) {
                        SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_TOKEN, token);
                        toHome();
                    }
                }
            }, new GofunErrorListener() {
                @Override
                public void onFailure(ResponseError error) {

                }
            });*/
            RequestOptService.login(LoginActivity.this, sim, code, new Response.Listener<NetBeanWrapper<UserRespBean>>() {
                @Override
                public void onResponse(NetBeanWrapper<UserRespBean> response) {
                    getProgressDialog().dismiss();

                    if (handleResponse(response)) {
                        return;
                    }
                    UserRespBean userRespBean = response.getModelData();
                    String token = userRespBean.getToken();
                    if (!CheckLogicUtil.isEmpty(token)) {
                        SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_TOKEN, token);
                        toHome();
                    }
                }
            },getDefaultErrorListener());
                    /*RequestOptService.login(LoginActivity.this, sim, code, new Response.Listener<NetBeanWrapper<UserRespBean>>() {
                        @Override
                        public void onResponse(NetBeanWrapper<UserRespBean> response) {
                            getProgressDialog().dismiss();

                            if (handleResponse(response)) {
                                return;
                            }
                            UserRespBean userRespBean = response.getModelData();
                            String token = userRespBean.getToken();
                            if (!CheckLogicUtil.isEmpty(token)) {
                                SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_TOKEN, token);
                                toHome();
                            }
                        }
                    }, getDefaultErrorListener());*/

            /*@Override
                public void onResponse(ResponseEntity response) {
                    getProgressDialog().dismiss();

                    if(handleResponse(response)){
                        return;
                    }
                    String token = response.modelData.get("token").toString();
                    if (!CheckLogicUtil.isEmpty(token)) {
                        SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_TOKEN, token);
                        toHome();
                    }
                }*/
        }

    }


//    private Response.Listener<ResponseEntity> loginListener = new Response.Listener<ResponseEntity>() {
//        @Override
//        public void onResponse(ResponseEntity response) {
//            DialogUtil.hideIndeterminateProgress(getProgressDialog());
//            UserInfo userInfo = JSON.parseObject(JSON.toJSONString(response.modelData.get("userInfo")), UserInfo.class);
//
//            SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_NICKNAME, userInfo.nickName);
//            SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_HEAD_IMG, userInfo.headImg);
//            SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_PHONE, userInfo.sim);
//
//            SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_CREDIT_STATE, CreditCheckState.getCheckStateName(Integer.valueOf(userInfo.verifyCredit)));
//            if (CheckLogicUtil.isEmpty(SharedPreferenceUtil.getStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PROP_FIRST_LOGIN, ""))) {//只是第一次登陆的时候赋值
//                SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PROP_FIRST_LOGIN, "yes");//是第一次登陆
//            }
//
//            if (CheckLogicUtil.isEmpty(SharedPreferenceUtil.getStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PUSH_SWITCH, ""))) {//只是第一次登陆的时候赋值
//                SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PUSH_SWITCH, "false");//没有点击暂不开启按钮
//            }
//            //双证提交审核
//            if (CheckLogicUtil.isEmpty(SharedPreferenceUtil.getStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PUSH_USRR_CHECK_ID, ""))) {//只是第一次登陆的时候赋值
//                SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PUSH_USRR_CHECK_ID, "false");//没有点击暂不开启按钮
//            }
//            //个人中心资质认证
//            if (CheckLogicUtil.isEmpty(SharedPreferenceUtil.getStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PUSH_QUALIFCATION_ID, ""))) {//只是第一次登陆的时候赋值
//                SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.PUSH_QUALIFCATION_ID, "false");//没有点击暂不开启按钮
//            }
//
//            String token = response.modelData.get("token").toString();
//            if (!CheckLogicUtil.isEmpty(token)) {
//                SharedPreferenceUtil.setStringSPAttrs(LoginActivity.this, SharedPreferenceUtil.AttrInfo.USER_TOKEN, token);
//            }
//
//            if (getIntent().getIntExtra(Constants.LoginForward.KEY, 0) == Constants.LoginForward.HOME_ACTIVITY) {
//                toHome();
//            } else {
//                stepToLoginForward(getIntent().getIntExtra(Constants.LoginForward.KEY, Constants.LoginForward.NO_FORWARD), getIntent().getExtras());
//                finish();
//            }
//        }
//    };

    private void toHome() {
        Intent intent = new Intent();
        intent.setClass(LoginActivity.this, NormalHomeActivity.class);
        intent.setAction(Constants.Action.HOME_REFRESH_ACTION);
        LoginActivity.this.startActivity(intent);
        LoginActivity.this.finish();
    }

    @OnClick({R.id.login_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                login();
                break;
        }
    }
}
